
import { EventBase } from "../../cccframework/Common/Components/EventBase";
import UITouchScript from "../../cccframework/Common/Components/UITouchScript";
import { gameHelper } from "../../cccframework/Utils/GameHelpler";

const { ccclass, property, executeInEditMode } = cc._decorator;

@ccclass
// @executeInEditMode
export default class RemoteSensingControl extends EventBase {

    @property({
        displayName: '中心点可否移动'
    })
    isMoveAnchor: boolean = false;

    @property(cc.Node)
    anchorNode: cc.Node = null;
    @property(cc.Node)
    moveNode: cc.Node = null;
    maxDistance: number = 30;

    onLoad() {
        super.onLoad && super.onLoad()
        let UIcamera = UITouchScript.getUICameraTouchScript().node;
        this.node.parent = UIcamera;

    }

    touchStart(touch: cc.Event.EventTouch) {
        if (this.isMoveAnchor) {
            this.anchorNode.position = this.anchorNode.parent.convertToNodeSpaceAR(gameHelper.getRealTouchWpos(touch));
        }
        this.move(touch);
    }
    touchMove(touch: cc.Event.EventTouch) {
        this.move(touch);
        touch.getLocation();
    }
    touchOver(touch: cc.Event.EventTouch) {
        this.moveNode.position = cc.v2();
        this.moveDir = cc.v2()
    }
    isGetServerPermissionUpdate: boolean = false;
    moveDir: cc.Vec2 = cc.v2();
    move(touch: cc.Event.EventTouch) {
        let anchorWpos = gameHelper.getWpos(this.anchorNode);
        let touchWpos = touch.getLocation();
        let dir = touchWpos.sub(anchorWpos).normalize();
        let dist = touchWpos.sub(anchorWpos).mag();
        if (dist > this.maxDistance) {
            dist = this.maxDistance;
        }
        this.moveNode.position = dir.mul(dist);
        this.moveDir = dir;
    }


}
